package sshFile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Jxrst {

	
	 public static File createCSVFile(List<Object> head, List<List<Object>> dataList,String outPutPath, String filename) {
		 
	        File csvFile = null;
	        BufferedWriter csvWtriter = null;
	        try {
	            csvFile = new File(outPutPath + File.separator + filename + ".csv");
	            File parent = csvFile.getParentFile();
	            if (parent != null && !parent.exists()) {
	                parent.mkdirs();
	            }
	            csvFile.createNewFile();
	 
	            // GB2312ʹ��ȷ��ȡ�ָ���","
	            csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
	                    csvFile), "UTF-8"), 1024);
	            // д���ļ�ͷ��
	            writeRow(head, csvWtriter);
	 
	            // д���ļ�����
	            for (List<Object> row : dataList) {
	                writeRow(row, csvWtriter);
	            }
	            csvWtriter.flush();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                csvWtriter.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return csvFile;
	    }
	 
	    /**
	     * дһ�����ݷ���
	     * @param row
	     * @param csvWriter
	     * @throws IOException
	     */
	    private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
	        // д���ļ�ͷ��
	        for (Object data : row) {
	            StringBuffer sb = new StringBuffer();
	            String rowStr = sb.append(data).append(",").toString();
	            csvWriter.write(rowStr);
	        }
	        csvWriter.newLine();
	    }
	 
	 
	    
	    
	    
	    public static void createCSVtool(String filepath) {
	    	List<Object> exportData = new ArrayList<Object>();
	        exportData.add("网元");         
	        exportData.add("序列号");
	      
	        Map<String,String>  wymap=new HashMap<>();
	        Map<String,String>  xlmap=new HashMap<>();
	        File file=new File(filepath);
	     
			try {
				
			InputStreamReader in = new InputStreamReader(new FileInputStream(file),"GBK");
				
			BufferedReader	bf = new BufferedReader(in);      
	        String s=null;
	        StringBuffer sf=new StringBuffer();
	        String wy="";
	        while((s = bf.readLine())!=null){	        	  
	               if(s.startsWith("网元 :")){
	            	   wy= s.split(":")[1];
	            	//   sf.append(wy.trim()).append(",");
	            	   wymap.put(wy, "网元");
	            	
	               } 
	               if(s.startsWith("设备序列号") ){
	            	   String  sernum= s.split("=")[1];
	            	   xlmap.put(wy, sernum);
	//            	   String  sernum= s.split("\\s+")[1];
	//            	   sf.append(sernum.trim()).append(";");
	            	
	               } 
	            
	           
	            }
	           
	        bf.close();   
	        System.out.println(sf.toString());
	       List<List<Object>> datalist = new ArrayList<List<Object>>();
	       
	    /*        String[] sp = sf.toString().split(";");
	        for (String line : sp) {
	        	List<Object> data=new LinkedList<Object>();
				String[] sl = line.split(",");
				data.add(sl[0]);
				data.add(sl[1]);
				datalist.add(data);
			}
	        */
	       for (String w: wymap.keySet()) {
	    	   List<Object> data=new LinkedList<Object>();
	    	   data.add(w);
	    	   if(xlmap.containsKey(w)){
	    		   data.add(xlmap.get(w));
	    	   }
	    	   datalist.add(data);
	    	   
		}
	       
	       
	        String path = "C:\\huawutongji\\rst\\";
	        String fileName = "ESN";
	        createCSVFile(exportData, datalist, path, fileName);
	        
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	    
	    public static void main(String[] args) {
			
	    	String fileth="C:\\huawutongji\\rst\\esn123_20210124151838_NJHQ_1077.rst";
	    	createCSVtool(fileth);
		}
	   
}
